//
//  SGExploreActiveVC.swift
//  songguo
//
//  Created by 潘传洲 on 16/6/3.
//  Copyright © 2016年 pcz. All rights reserved.
//

import UIKit

private let ID = "Topic"

class SGExploreVC: UIViewController {

    // 状态栏view
    lazy private var stayusView : UIView = {
        let stayusView = UIView()
        stayusView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: 20)
        stayusView.layer.contents = UIImage(named: "nav_bar_bg_2x64_")?.CGImage
        return stayusView
    }()
    
    //scrollView
    lazy private var contentScrollView : UIScrollView = {
       let contentScrollView = UIScrollView()
        contentScrollView.frame = UIScreen.mainScreen().bounds
        contentScrollView.height += 64
        contentScrollView.y = -64
        return contentScrollView
    }()
    
    // scrollViewd的内容视图
    lazy private var contentView : UIView = {
        let contentView = UIView()
        contentView.backgroundColor = UIColor(patternImage: UIImage(named: "nav_bar_bg_2x64_")!)
        return contentView
    }()
    
//    // 取消按钮
//    lazy private var cancelBtn : UIButton = {
//        let cancelBtn = UIButton(type: .Custom)
//        cancelBtn.setTitle("取消", forState: .Normal)
//        cancelBtn.setTitleColor(UIColor.color(99, g: 99, b: 99), forState: .Normal)
//        cancelBtn.titleLabel?.font = UIFont.systemFontOfSize(14)
//        cancelBtn.addTarget(self, action: #selector(cancle), forControlEvents: .TouchUpInside)
//        cancelBtn.sizeToFit()
//        
//        return cancelBtn
//    }()
    
    //分类视图
    lazy private var classificationView : SGClassificationView = {
        let classificationView = SGClassificationView(frame: CGRect(x: 0, y: CGRectGetMaxY(self.searchView.frame), width: screenWidth, height: 130))
        classificationView.delegate = self
        return classificationView
    }()
    
    
    //生活家视图
    lazy private var autorView : SGAuthorView = {
        let autorView = SGAuthorView()
        autorView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.classificationView.frame) + 5, width: screenWidth, height: 130)
        return autorView
    }()
    
    
    //话题视图
    lazy private var topicView : SGTopicView = {
        let topicView = SGTopicView()
        topicView.frame = CGRect(x: 0, y: CGRectGetMaxY(self.autorView.frame) + 5, width: screenWidth, height: 145 * 3 + 30)
        topicView.registerNib(UINib.init(nibName: "SGTopicTableViewCell", bundle: nil), forCellReuseIdentifier: ID)
        
        topicView.dataSource = self
        topicView.delegate = self
        return topicView
    }()
    
    // 搜索框
    lazy private var searchView : UIView = {
        let searchView = UIView()
        searchView.frame = CGRect(x: 0, y: 20, width: screenWidth, height: 44)
        searchView.layer.contents = UIImage(named: "nav_bar_bg_2x64_")?.CGImage
        
        let searchBar = SGExploerSearchBar(frame: searchView.bounds)
        searchBar.delegate = self
        searchView.addSubview(searchBar)
        return searchView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.whiteColor()
        setUpView()
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.sharedApplication().keyWindow?.addSubview(stayusView)
    }
    
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)
        stayusView.removeFromSuperview()
    }
    
    func setUpView() {
        navigationController?.navigationBar.removeFromSuperview()
        
        view.addSubview(contentScrollView)
        // 内容View
        contentScrollView.addSubview(contentView)
        // 搜索框
        contentView.addSubview(searchView)
        
        //分类视图
        contentView.addSubview(classificationView)
        
        // 生活家视图
        contentView.addSubview(autorView)
        
        // 话题视图
        contentView.addSubview(topicView)
        
        // 设置scroll的滚动区域
        contentView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: CGRectGetMaxY(topicView.frame))
        contentScrollView.contentSize = contentView.size
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        cancle()
    }
    
    func cancle() {
        UIApplication.sharedApplication().keyWindow?.endEditing(true)
    }
    
}

extension SGExploreVC: UISearchBarDelegate{
    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        print(#function)
        return true
    }
    
    func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool {
        print(#function)
        return true
    }
}

extension SGExploreVC: UITableViewDelegate{
    
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        if indexPath.row == 2 {
            return 140
        }else{
            return 145
        }
    }
}

extension SGExploreVC: UITableViewDataSource{
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ID, forIndexPath: indexPath) as! SGTopicTableViewCell
        
//        cell.item = homeListModels![indexPath.row]
        
        return cell
    }
}


extension SGExploreVC : SGClassificationViewDelegate{
    func chengeFrame() {
        
        UIView.animateWithDuration(0.25) { 
            if self.classificationView.height == 130 {
                self.classificationView.height = 400
            }else{
                self.classificationView.height = 130
            }
            self.autorView.y = CGRectGetMaxY(self.classificationView.frame) + 5
            self.topicView.y = CGRectGetMaxY(self.autorView.frame) + 5
            self.contentView.height = CGRectGetMaxY(self.topicView.frame)
            self.contentScrollView.contentSize = self.contentView.size
        }
        
    }
}
